
export OS ?= linux
export HOST ?= x86_64
export DEVNAME ?= x86_64
export HTTPS_SUPPORT ?=1
export USE_WOLFSSL ?=1

CC := $(CROSSCOMPILE)gcc
AR := $(CROSSCOMPILE)ar


LIBWD_DIR=$(CURDIR)/libwd
include ./libwd/Makefile.inc

CFLAGS += -Wall -I./dial_shell/
LDFLAGS += -pthread

export EXEC_probe ?= onuProbe
export EXEC_loader ?= onuLoader
export EXEC_monitor ?= onuMonitor

SRCS=$(wildcard *.c *.h)
SRCS_loader=$(filter-out main.c onuMonitor.c net_info.c router_mode.c, $(SRCS))
SRCS_probe=$(filter-out onuLoader.c onuMonitor.c, $(SRCS))
OBJS_loader=$(subst .c,.o,$(SRCS_loader))
OBJS_probe=$(subst .c,.o,$(SRCS_probe))
OBJS_Dial_Shell= dial_shell/*.o

LDFLAGS_loader =$(OBJS_Dial_Shell) \
				$(LIBWD_DIR)/libwdplatform/devinfo.o \
				$(LIBWD_DIR)/libwdplatform/hg261gs.o  \
				$(LIBWD_DIR)/libwdplatform/pt622g.o  \
				$(LIBWD_DIR)/libwdplatform/TEWA-500AG.o  \
				$(LIBWD_DIR)/libwdplatform/linux_info.o $(LIBWD_DIR)/libwdplatform/android_info.o \
				$(LIBWD_DIR)/libwdplatform/trans.o $(LIBWD_DIR)/libwdplatform/comm_param.o \
				$(LIBwdbase_static) $(LIBghttp_static) \
				$(LIBcjson_static) $(LIBwolfssl_static) $(LDFLAGS) 
ifeq ($(HTTPS_SUPPORT),1)
ifeq ($(USE_WOLFSSL),1)
LDFLAGS_loader +=
else
LDFLAGS_loader += -lssl -lcrypto
endif
endif

DATE_STR=$(shell date +"%Y%m%d%H%M")
VERSION_STR=$(shell grep "ONU_SW_VERSION" onu_comm.h | cut -d' ' -f3)
TMP_TAR_FILE_NAME = onuProbe.$(OS).$(DEVNAME)-$(HOST).\(test\).tar.gz
RELEASE_TAR_FILE_NAME = onuProbe.release.$(OS).$(DEVNAME)-$(HOST).\($(VERSION_STR)-$(DATE_STR)\).tar.gz

all:pre_update_version dial_shell libwd $(EXEC_probe) $(EXEC_loader) $(EXEC_monitor)
	
pre_update_version:
	@sed -i 's/.*ONU_SW_DATE.*/#define ONU_SW_DATE "${DATE_STR}"/' onu_comm.h;

dial_shell:
	make -C dial_shell

$(EXEC_probe):$(OBJS_probe)
	$(CC) $(CFLAGS) $(CFLAGS_WD) $(OBJS_probe) -o $@  $(LDFLAGS) $(LDFLAGS_WD) $(ENV_LDFLAGS)

$(EXEC_loader):$(OBJS_loader)
	$(CC) $(CFLAGS) $(CFLAGS_WD) $(OBJS_loader) -o $@  $(LDFLAGS_loader) $(ENV_LDFLAGS)
$(EXEC_monitor):onuMonitor.o onu_comm.o
	$(CC) $(CFLAGS) $(CFLAGS_WD) onuMonitor.o onu_comm.o -o $@ $(LDFLAGS) $(LDFLAGS_WD) $(ENV_LDFLAGS)
libwd:
	make -C $(LIBWD_DIR)
%.o:%.c
	$(CC) -c $(CFLAGS) $(CFLAGS_WD) $< -o $@ 
test:
	make -C test
clean:
	rm $(EXEC_probe) $(EXEC_loader) $(EXEC_monitor) -f;
	rm ./bin -rf;
	rm *.o -f;
	rm *.log -f;
	make clean -C libwd;
	make clean -C dial_shell
	make clean -C test
distclean:clean
	make distclean -C libwd

tar:
	# tar zcvf dial_shell.tar.gz dial_shell;
	tar zcvf $(TMP_TAR_FILE_NAME) $(EXEC_probe) $(EXEC_loader) $(EXEC_monitor);

release:
	tar zcvf $(RELEASE_TAR_FILE_NAME) $(EXEC_probe) $(EXEC_loader) $(EXEC_monitor);

.PHONY: all clean distclean libwd tar dial_shell test pre_update_version release
